import xml.etree.ElementTree as ET

# 医疗床相关
stats = ["BedRestEffectiveness", "Comfort", "ImmunityGainSpeedFactor", "MedicalTendQualityOffset",
         "SurgerySuccessChanceFactor","InjuryHealingFactor"]

# 需要反转计数的属性, 近战冷却, 远程冷却
reverse_stats = []
# 近战冷却, 远程冷却
# reverse_stats = reverse_stats + ["MeleeCooldownFactor", "RangedCooldownFactor"]
# 近战冷却, 远程冷却也有可能要用这两个属性
reverse_stats = reverse_stats + ["MeleeWeapon_CooldownMultiplier","RangedWeapon_Cooldown"]
# 护甲相关
stats = stats + ["ArmorRating_Blunt", "ArmorRating_Sharp", "ArmorRating_Heat"]
# 近战穿透
stats = stats + ["MeleeArmorPenetration"]
# 研究速度 学习速度 谈判能力 价格改善
stats = stats + ["ResearchSpeed","GlobalLearningFactor","NegotiationAbility","TradePriceImprovement"]
# 全局工作速度 采矿产量 动物产品 一般工作速度
stats = stats + ["WorkSpeedGlobal","MiningYield","AnimalGatherYield","GeneralLaborSpeed"]
# 工作速度系数和研究速度系数，由于工作台没有品质，不能加
# stats = stats + ["WorkTableWorkSpeedFactor","ResearchSpeedFactor"]
# 反向计算的也加进来
stats = stats + reverse_stats

likeys = ["factorAwful", "factorPoor", "factorNormal", "factorGood", "factorExcellent", "factorMasterwork",
          "factorLegendary"]
livals = [0.29, 0.49, 0.74, 1, 1.51, 2.01, 3.01]

root = ET.Element("Patch")


def nomatch(stat):
    nomatch_node = ET.Element("nomatch")
    nomatch_node.set("Class", "PatchOperationAdd")
    xpatch = ET.Element("xpath")
    xpatch.text = f"/Defs/StatDef[defName = \"{stat}\"]"
    value = ET.Element("value")
    parts = ET.Element("parts")
    # parts.append(li(stat))
    value.append(parts)
    nomatch_node.append(xpatch)
    nomatch_node.append(value)
    return nomatch_node


def li(stat):
    tlist = livals.copy()
    if reverse_stats.__contains__(stat):
        tlist.reverse()
    li = ET.Element("li")
    li.set("Class", "StatPart_Quality")
    for key, val in zip(likeys, tlist):
        likey = ET.Element(key)
        likey.text = str(val)
        li.append(likey)
    return li


def test(key, stat):
    keyel = ET.Element(key)
    if "match" == key:
        keyel.set("Class", "PatchOperationReplace")
        xpatch = ET.Element("xpath")
        xpatch.text = f"/Defs/StatDef[defName = \"{stat}\"]/parts/li[Class = \"StatPart_Quality\"]"
        keyel.append(xpatch)
    else:
        keyel.set("Class", "PatchOperationAdd")
        xpatch = ET.Element("xpath")
        xpatch.text = f"/Defs/StatDef[defName = \"{stat}\"]/parts"
        keyel.append(xpatch)
    value = ET.Element("value")
    value.append(li(stat))
    keyel.append(value)
    return keyel


def macth(stat):
    mactht_node = ET.Element("li")
    mactht_node.set("Class", "PatchOperationConditional")
    xpatch = ET.Element("xpath")
    xpatch.text = f"/Defs/StatDef[defName = \"{stat}\"]/parts/li[Class = \"StatPart_Quality\"]"
    mactht_node.append(xpatch)
    mactht_node.append(test("match", stat))
    mactht_node.append(test("nomatch", stat))
    return mactht_node


for stat in stats:
    stat_node = ET.Element("Operation")
    stat_node.set("Class", "PatchOperationSequence")
    operations = ET.Element("operations")
    oli = ET.Element("li")
    oli.set("Class", "PatchOperationConditional")
    xpath_node = ET.Element("xpath")
    xpath_node.text = f"/Defs/StatDef[defName = \"{stat}\"]/parts"
    oli.append(xpath_node)
    oli.append(nomatch(stat))
    operations.append(oli)
    operations.append(macth(stat))
    stat_node.append(operations)
    root.append(stat_node)

xml_str = ET.tostring(root, encoding="utf-8", method="xml").decode("utf-8")
print(xml_str)
tree = ET.ElementTree(root)
tree.write("../Mods/QualityMatters/Patches/guxing_mypath.xml", encoding="utf-8", xml_declaration=True)
